package com.lanyuan.controller.system;

import com.lanyuan.controller.index.BaseController;
import com.lanyuan.entity.ServerInfoFormMap;
import com.lanyuan.mapper.ServerInfoMapper;
import com.lanyuan.plugin.PageView;
import com.lanyuan.util.Common;
import com.lanyuan.util.PropertiesUtils;
import com.lanyuan.util.SystemInfo;
import org.hyperic.sigar.Sigar;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lanyuan 2014-11-19
 * @version 3.0v
 */
@Controller
@RequestMapping("/monitor/")
public class MonitorController extends BaseController {

    @Inject
    private ServerInfoMapper serverInfoMapper;

    @RequestMapping("list")
    public String listUI() throws Exception {
        return Common.BACKGROUND_PATH + "/system/monitor/list";
    }

    @ResponseBody
    @RequestMapping("findByPage")
    public PageView findByPage(String pageNow, String pageSize) throws Exception {
        ServerInfoFormMap serverInfoFormMap = getFormMap(ServerInfoFormMap.class);
        serverInfoFormMap = toFormMap(serverInfoFormMap, pageNow, pageSize, serverInfoFormMap.getStr("orderby"));
        pageView.setRecords(serverInfoMapper.findByPage(serverInfoFormMap));
        return pageView;
    }

    @RequestMapping("info")
    public String info(Model model) throws Exception {
        model.addAttribute("cpu", PropertiesUtils.findPropertiesKey("cpu"));
        model.addAttribute("jvm", PropertiesUtils.findPropertiesKey("jvm"));
        model.addAttribute("ram", PropertiesUtils.findPropertiesKey("ram"));
        model.addAttribute("toEmail", PropertiesUtils.findPropertiesKey("toEmail"));
        return Common.BACKGROUND_PATH + "/system/monitor/info";
    }

    @RequestMapping("monitor")
    public String monitor() throws Exception {
        return Common.BACKGROUND_PATH + "/system/monitor/monitor";
    }

    @RequestMapping("systemInfo")
    public String systemInfo(Model model) throws Exception {
        model.addAttribute("systemInfo", SystemInfo.SystemProperty());
        return Common.BACKGROUND_PATH + "/system/monitor/systemInfo";
    }

    @ResponseBody
    @RequestMapping("usage")
    public ServerInfoFormMap usage(Model model) throws Exception {
        return SystemInfo.usage(new Sigar());
    }

    /**
     * 修改配置
     */
    @ResponseBody
    @RequestMapping("/modifySer")
    public Map<String, Object> modifySer(String key, String value) throws Exception {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        try {
            // 从输入流中读取属性列表（键和元素对）
            PropertiesUtils.modifyProperties(key, value);
        } catch (Exception e) {
            dataMap.put("flag", false);
        }
        dataMap.put("flag", true);
        return dataMap;
    }
}